
// <copyright file="DataReaderToDataRecordEnumerableAdapter.cs" company="Subhadra Software Solutions">
//   
// </copyright>
// <summary>
//   The data reader to data record enumerable adapter.
// </summary>

namespace SubhadraSolutions.Sharp.Utils.Data
{
    using System.Collections;
    using System.Collections.Generic;
    using System.Data;

    /// <summary>
    /// The data reader to data record enumerable adapter.
    /// </summary>
    public sealed class DataReaderToDataRecordEnumerableAdapter : IEnumerable<IDataRecord>
    {
        #region Fields

        /// <summary>
        /// The reader.
        /// </summary>
        private readonly IDataReader _reader;

        #endregion Fields

        #region Constructors and Finalizers

        /// <summary>
        /// Initializes a new instance of the <see cref="DataReaderToDataRecordEnumerableAdapter"/> class.
        /// </summary>
        /// <param name="reader">
        /// The reader.
        /// </param>
        public DataReaderToDataRecordEnumerableAdapter(IDataReader reader)
        {
            this._reader = reader;
        }

        #endregion Constructors and Finalizers

        #region Public Methods and Operators

        /// <summary>
        /// The get enumerator.
        /// </summary>
        /// <returns>
        /// The <see cref="IEnumerator"/>.
        /// </returns>
        public IEnumerator<IDataRecord> GetEnumerator()
        {
            return new DataReaderToDataRecordEnumeratorAdapter(this._reader);
        }

        #endregion Public Methods and Operators

        #region Explicit Interface Methods

        /// <summary>
        /// The get enumerator.
        /// </summary>
        /// <returns>
        /// The <see cref="IEnumerator"/>.
        /// </returns>
        IEnumerator IEnumerable.GetEnumerator()
        {
            return this.GetEnumerator();
        }

        #endregion Explicit Interface Methods

        /// <summary>
        /// The data reader to data record enumerator adapter.
        /// </summary>
        private sealed class DataReaderToDataRecordEnumeratorAdapter : IEnumerator<IDataRecord>
        {
            #region Fields

            /// <summary>
            /// The reader.
            /// </summary>
            private readonly IDataReader reader;

            #endregion Fields

            #region Constructors and Finalizers

            /// <summary>
            /// Initializes a new instance of the <see cref="DataReaderToDataRecordEnumeratorAdapter"/> class.
            /// </summary>
            /// <param name="reader">
            /// The reader.
            /// </param>
            public DataReaderToDataRecordEnumeratorAdapter(IDataReader reader)
            {
                this.reader = reader;
            }

            #endregion Constructors and Finalizers

            #region Public Properties

            /// <summary>
            /// Gets the current.
            /// </summary>
            public IDataRecord Current
            {
                get
                {
                    return this.reader;
                }
            }

            #endregion Public Properties

            #region Explicit Interface Properties

            /// <summary>
            /// Gets the current.
            /// </summary>
            object IEnumerator.Current
            {
                get
                {
                    return this.reader;
                }
            }

            #endregion Explicit Interface Properties

            #region Public Methods and Operators

            /// <summary>
            /// The dispose.
            /// </summary>
            public void Dispose()
            {
            }

            /// <summary>
            /// The move next.
            /// </summary>
            /// <returns>
            /// The <see cref="bool"/>.
            /// </returns>
            public bool MoveNext()
            {
                return this.reader.Read();
            }

            /// <summary>
            /// The reset.
            /// </summary>
            public void Reset()
            {
            }

            #endregion Public Methods and Operators
        }
    }
}